package licode.unisop.auth.application.uniclient;

import licode.unisop.auth.application.assembler.AuthClientAssembler;
import licode.unisop.auth.client.info.*;
import licode.unisop.client.api.auth.SopUserAuth;
import licode.unisop.client.info.*;
import licode.unisop.client.vo.SopAuthContext;
import licode.unisop.client.vo.SopAuthResult;
import licode.unisop.client.vo.SopAuthUser;
import licode.unisop.client.vo.SopCredential;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 登录用户操作接口
 * @author licode
 */
@Primary
@Service
public class SopUserAuthImpl implements SopUserAuth {
    @Resource
    private SopAuthorExecutor sopAuthorExecutor;

    @Resource
    private AuthClientAssembler authClientAssembler;

    @Override
    public Map<String, Object> getCaptchaCode() {
        return null;
    }

    @Override
    public String updatePassword(SopUpdatePasswordIn info) {
        return sopAuthorExecutor.updatePassword(info);
    }

    @Override
    public SopAuthResult authorize(SopAuthIn authAccount) {
        return sopAuthorExecutor.authorize(buildLoginContext(authAccount));
    }

    @Override
    public SopAuthResult authorizeDirect(SopAuthDirectIn accReq) {
        return sopAuthorExecutor.authorizeDirect(accReq);
    }

    @Override
    public SopAuthUser token(SopCodeInfo codeInfo) {
        return sopAuthorExecutor.token(LoginCodeIn.builder()
                .code(codeInfo.getCode())
                .state(codeInfo.getCode())
                .authDetail(AuthDetailIn.builder()
                        .realmId(codeInfo.getRealmId())
                        .tenantId(codeInfo.getTenantId())
                        .platformId(codeInfo.getPlatformId())
                        .build())
                .build());
    }

    @Override
    public SopAuthUser login( SopAuthIn authAccount) {
        return sopAuthorExecutor.login(buildLoginContext(authAccount));
    }

    @Override
    public SopAuthUser loginDirect(SopAuthDirectIn directIn) {
        return sopAuthorExecutor.loginDirect(directIn);
    }

    @Override
    public SopCredential refresh(SopTokenInfo sopTokenInfo) {
        Credential credential = sopAuthorExecutor.refresh(sopTokenInfo);
        return authClientAssembler.fromCredential(credential);
    }

    @Override
    public void logout(SopTokenInfo authToken) {
        sopAuthorExecutor.logout(authToken);
    }

    @Override
    public void logoutUser(SopUserIdIn req) {
        sopAuthorExecutor.logoutUser(req);
    }

    @Override
    public void logoutSession(SopTokenInfo authToken) {
        sopAuthorExecutor.logoutSession(authToken);
    }

    private LoginContextIn buildLoginContext(SopAuthIn authAccount) {
        SopAuthContext context = authAccount.getContext();

        return LoginContextIn.builder()
                .account(AccountIn.builder()
                        .realmId(context.getRealmId())
                        .category(authAccount.getCategory())
                        .account(authAccount.getAccount())
                        .build())
                .auth(IdentityIn.builder()
                        .authCode(authAccount.getAuthCode())
                        .authType(authAccount.getAuthType())
                        .authData(authAccount.getAuthData())
                        .build())
                .authDetail(AuthDetailIn.builder()
                        .realmId(context.getRealmId())
                        .tenantId(context.getTenantId())
                        .terminal(context.getTerminalCode())
                        .platformId(context.getPlatformId())
                        .clientId(context.getClientCode())
                        .clientSecret(context.getClientSecret())
                        .build())
                .build();
    }
}
